module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        cachebuster: {
            options: {
                basedir: 'src/js/',
                length: 4,
                formatter: function(hashes) {
                    var _re = /\\/gi;
                    var output = {};
                    for(var filename in hashes) {
                        var _filename = filename.replace(_re, '/');
                        var _n1 = _filename.lastIndexOf('/') + 1;
                        var _n2 = _filename.lastIndexOf('.');
                        var __f = '';
                        var _n3 = _filename.indexOf('modules/'),
                            _n4 = _filename.indexOf('lib/'),
                            _n5 = _filename.indexOf('data/'),
                            _n6 = _filename.indexOf('common/');
                        if(_n3 === 0 || _n4 === 0 || _n5 === 0 || _n6 === 0) {
                            __f = _filename.substring(_n1, _n2);
                        } else {
                            var _a = (_filename.split('.'))[0];
                            __f = _a.replace(/\//gi, '_');
                        }
                        if(__f == 'jquery') {
                            output[__f] = _filename;
                        } else {
                            output[__f] = _filename + '?v=' + hashes[filename] + '.js';
                        }
                    }
                    grunt.file.write('.build/hash.json', JSON.stringify(output));
                    return JSON.stringify(output);
                }
            },
            build: {
                src: ['src/js/**/*.js', '!src/js/config.js', '!src/js/**/hash.js', '!src/js/lib/seajs/sea.js'],
                dest: '.build/_hash.json'
            }
        },
        // Task jsmin
        uglify: {
            options: {
                // compress: false,
                compress: {
                    drop_console: true
                },
                mangle: false,
                preserveComments: 'some'
            },
            build: {
                files: [{
                    expand: true,
                    cwd: 'src/js', //js目录下
                    src: ['**/*.js', '!**/.svn', '!**/.idea', '!**/*.db', '!**/hash.js'], //所有js文件
                    dest: 'static/js' //输出到此目录下
                }]
            }
        },
        copy: {
            js: {
                cwd: 'src/js',
                src: ['**', '!**/.svn', '!**/.idea', '!**/*.db', '!**/hash.js'],
                dest: 'static/js',
                expand: true
            },
            img: {
                cwd: 'src/images',
                src: ['**/*', '!**/.svn', '!**/.idea', '!**/*.db', '!**/*.psd'],
                dest: 'static/images',
                expand: true
            },
            css: {
                cwd: 'src/css',
                src: ['**/*', '!**/.svn', '!**/.idea', '!**/*.db'],
                dest: 'static/css',
                expand: true
            }
        },
        less: {
            dev: {
                files: [{
                    expand: true,
                    cwd: 'src/less',
                    src: ['**/*.less', '!**/base.less', '!**/mod-**.less'],
                    dest: 'static/css',
                    ext: '.css'
                }]
            },
            online: {
                files: [{
                    expand: true,
                    cwd: 'src/less',
                    src: ['**/*.less', '!**/base.less', '!**/mod-**.less'],
                    dest: 'static/css',
                    ext: '.css'
                }]
            }
        },
        clean: ['static', '.build', 'src/css'],
        watch: {
            js: {
                files: ['src/js/**/*.js', '!**/.svn', '!**/.idea', '!**/*.db', '!**/hash.js'],
                tasks: ['cachebuster', 'copy:js'],
                options: {
                    debounceDelay: 100,
                    spawn: false
                }
            },
            less: {
                files: "src/less/**/*.less",
                tasks: ['less:dev']
            },
            css: {
                files: ['src/css/**/*.css'],
                tasks: ['copy:css'],
                options: {
                    spawn: false
                }
            },
            image: {
                files: ['src/images/**/*', '!**/.svn', '!**/.idea', '!**/*.db', '!**/*.psd'],
                tasks: ['copy:img'],
                options: {
                    spawn: false
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-cachebuster');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-clean');

    grunt.registerTask('default', ['clean', 'cachebuster', 'uglify', 'less:online', 'copy:img','copy:js']);
    grunt.registerTask('dev', ['cachebuster', 'copy:js', 'less:dev', 'copy:img']);
};
